﻿#include "dd.h"
#include "DrawGridlineWidget.h"
#include <QFileDialog>
#include <QMessageBox>
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"
#include "ImageHandler.h"
#include <QMouseEvent>
#include <QDebug>
#include <QTimer>
#include <QColorDialog>

dd::dd(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	connect(ui.graphicsView, &CustomGraphicsView::sigMouseInImage, this, [this](QPointF pointF)
	{
		ui.label_xy->setText(QString("X:%1 Y:%2").arg((int)pointF.x()).arg((int)pointF.y()));
	});
}

dd::~dd()
{

}

void dd::on_btn_choose_clicked()
{
	QString selected_file = QFileDialog::getOpenFileName(this, tr("选择图片"));

	if (selected_file.isEmpty())
	{
		QMessageBox::warning(this, "警告", "选择为空");
		return;
	}

	origin_mat_ = cv::imread(selected_file.toLocal8Bit().toStdString());
	result_mat_ = origin_mat_.clone();
	refreshLabel();
}

void dd::on_btn_draw_clicked()
{
	//double dSpace = ui.sb_space->value() / 10.0;
	//int lineWidth = ui.sb_line_width->value();
	//cv::Mat mat = origin_mat_.clone();
	//cv::Scalar color(color_.blue(), color_.green(), color_.red());
	//// 画竖线
	//double xx = dSpace * 10;
	//while (xx < mat.cols)
	//{
	//	cv::Point pt1(xx, 0);  // 起点坐标
	//	cv::Point pt2(xx, mat.rows);  // 终点坐标
	//	cv::line(mat, pt1, pt2, color, lineWidth, cv::LINE_AA);

	//	xx += dSpace * 10;
	//}

	//// 画横线
	//double yy = dSpace * 10;
	//while (yy < mat.rows)
	//{
	//	cv::Point pt1(0, yy);  // 起点坐标
	//	cv::Point pt2(mat.cols, yy);  // 终点坐标
	//	cv::line(mat, pt1, pt2, color, lineWidth, cv::LINE_8);

	//	yy += dSpace * 10;
	//}

	//if (ui.cb->isChecked())
	//{
	//	int dLength = ui.sb_kd_length->value();
	//	double xxx = dSpace;
	//	while (xxx < mat.cols)
	//	{
	//		// 上边沿
	//		cv::Point pt1(xxx, 0);  // 起点坐标
	//		cv::Point pt2(xxx, dLength);  // 终点坐标
	//		cv::line(mat, pt1, pt2, color, lineWidth, cv::LINE_8);

	//		// 下边沿
	//		cv::Point pt3(xxx, mat.rows);  // 起点坐标
	//		cv::Point pt4(xxx, mat.rows - dLength);  // 终点坐标
	//		cv::line(mat, pt3, pt4, color, lineWidth, cv::LINE_8);

	//		xxx += dSpace;
	//	}

	//	double yyy = dSpace;
	//	while (yyy < mat.cols)
	//	{
	//		// 左边沿
	//		cv::Point pt1(0, yyy);  // 起点坐标
	//		cv::Point pt2(dLength, yyy);  // 终点坐标
	//		cv::line(mat, pt1, pt2, color, lineWidth, cv::LINE_8);

	//		// 右边沿
	//		cv::Point pt3(mat.cols, yyy);  // 起点坐标
	//		cv::Point pt4(mat.cols - dLength, yyy);  // 终点坐标
	//		cv::line(mat, pt3, pt4, color, lineWidth, cv::LINE_8);

	//		yyy += dSpace;
	//	}
	//}

	//result_mat_ = mat;
	//refreshLabel();
}

void dd::on_btn_output_clicked()
{
	QString selected_dir = QFileDialog::getExistingDirectory(this, tr("选择保存位置"), "", QFileDialog::ShowDirsOnly);
	if (selected_dir.isEmpty())
	{
		QMessageBox::warning(this, "警告", "选择为空");
		return;
	}

	//cv::cvtColor(result_mat_, result_mat_, cv::COLOR_RGB2BGR);
	cv::imwrite((selected_dir + "/result.png").toStdString(), result_mat_);
}

void dd::on_btn_draw_gradline_clicked()
{
	DrawGridlineWidget::getInstance()->show();
}

void dd::refreshLabel()
{
	QImage image = ImageHandler::cvMatToQImage(result_mat_.clone());
	//ui.label_image->resize(image.width(), image.height());
	QPixmap scaledPixmap = QPixmap::fromImage(image).scaled(ui.graphicsView->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
	
	ui.graphicsView->setPixmap(scaledPixmap);
}

void dd::mousePressEvent(QMouseEvent* ev)
{

}

void dd::mouseMoveEvent(QMouseEvent *)
{

}

void dd::mouseReleaseEvent(QMouseEvent* ev)
{

}
